//
//  ListCollectionViewController.swift
//  TestCollectionViewAnimation
//
//  Created by 薛永伟 on 2019/9/16.
//  Copyright © 2019 XueYongWei. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class ListCollectionViewController: UICollectionViewController,UICollectionViewDelegateFlowLayout {

    
    let colors = [
        UIColor.red,
        UIColor.purple,
        UIColor.blue,
        UIColor.lightGray,
        UIColor.green,
        UIColor.darkGray,
        UIColor.orange
    ]
    
    var ani = 0.5
    
    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
        
        // Register cell classes
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        
        
    }
    

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {

        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return 5
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
    
        cell.backgroundColor = colors[indexPath.row]
    
        if cell.frame.origin.x < view.bounds.size.width,self.ani > 0 {
            self.ani += 0.1
            
            UIView.animate(withDuration: 0) {
                cell.alpha = 0.0
                cell.transform = CGAffineTransform.init(translationX: self.view.bounds.size.width, y: 0)
            }
            
            UIView.animate(withDuration: self.ani ) {
                cell.alpha = 1.0
                cell.transform = CGAffineTransform.identity
            }
        }else{
            // 标记为不再动画
            self.ani = -1
        }
        
        
        return cell
    }

    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize.init(width: 200, height: 100)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        
        return 20
    }
    
    
    override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        
        let cell = collectionView.cellForItem(at: indexPath)
        UIView.animate(withDuration: 0.2) {
            cell?.transform = CGAffineTransform.init(scaleX: 0.9, y: 0.9)
        }
    }
    
    override func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        
        let cell = collectionView.cellForItem(at: indexPath)
        UIView.animate(withDuration: 0.2) {
            cell?.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)
        }
    }
   
}
